/**
 * Javascipt Library for handle page action
 * @Ray Li
 * js file for addprocess.html
 */

 /*----------------------------global variables declaration-----------------------*/  
var propertiesNodeDialog = null;
var propertiesTaskNodeDialog = null;
var propertiesDecisionNodeDialog = null;
var propertiesForkNodeDialog = null;
var propertiesJoinNodeDialog = null;
var postLoadDialog = null;
var processEventDialog = null;
var metadataDefinitionDialog = null;
var nodeValuesDialog = null;
var workFlowCloseDialog = null;
var xiorkFlow_close = null;
var workFlowSaveDialog = null;
var xiorkFlow_save = null;
var workflowName_save = null;
var doClose_save = null;
var closeFieldDefinitionDialog = null;


var processDefinitionList;
//
XiorkFlowWorkSpace.build();

//global error handler
dwr.engine.setErrorHandler(errorHandler);  

function errorHandler(errorString, exception){
    if(exception.cause){
	    alertMessage("操作失败！\r\n"+exception.cause.message);
	}else{
	    alertMessage("操作失败！\r\n"+errorString);
	}
	return;
}

/*----------------------------page initialization (after the page dom is ready)-----------------------*/
function init() {
	
	//load page template (header, nav, dialog etc.)
	cms.Dialog.LoadAlertMessageTemplate('processdialog.html','alertdialog-panel', Init_dialog);			
	//window.alertForm.alertOK.onclick = exitwindow;
	//cms.Dialog.LoadAlertMessageTemplate('header.html','header-panel', Init_header);
	
	// init the workflow designer
    Message.setOutter(Toolkit.getElementByID("message"));

    //
    var designerDiv = Toolkit.getElementByID("designer");
    var xiorkFlow = new XiorkFlow(designerDiv);
    xiorkFlow.setProcessList(window.dialogArguments);
    
   // var url= ListProcessDefinition();
	//new Ajax(url, setProcessDefinitionList);
   // document.title="work process";
	//
  //  document.body.onbeforeunload = function () {
   //    if (xiorkFlow.getWrapper().isChanged()) {
              //您对工作流程图的编辑尚未保存，离开该页面将退出系统!
      //      window.event.returnValue = "Exit design page!";
      //  }
  //  };
    
	//initialize the yui widget (dialog, button, tabview etc.)
	initYUIWidgets();  
}

if(navigator.appVersion.indexOf("MSIE 6.0")!=-1){ 
    window.onload=init;
}else{ 
    YAHOO.util.Event.onDOMReady(init);
} 

/*
 * initialize the yui widget (dialog, button, tabview etc.)
 */  
 function initYUIWidgets(){
		 
 }
 /*
  * initialize the page header
  */ 
 function Init_header(){}
 
 /*
  * initialize the page dialog
  */ 
 
 function Init_dialog(){
	    //handle cancel button event
        var handleCancel = function() {
		    this.cancel();
	    }; 
	    
	    //Instantiate the node property dialog 
		propertiesNodeDialog = new YAHOO.widget.Dialog("PropertiesNodeDialog", 
				{ width : "700px",
				  fixedcenter : true,
				  visible : false,    
                  constraintoviewport : true,
                  modal : true,
				  buttons : [ { text:"确定", handler:send, isDefault:true },
						      { text:"取消", handler:handleCancel } ]
				 } );	
        propertiesNodeDialog.render();
        
        //Instantiate the task node property dialog 
    	propertiesTaskNodeDialog = new YAHOO.widget.Dialog("PropertiesTaskNodeDialog", 
				{ width : "700px",
				  fixedcenter : true,
				  visible : false,    
                  constraintoviewport : true,
                  modal : true,
				  buttons : [ { text:"确定", handler:taskSend, isDefault:true },
						      { text:"取消", handler:handleCancel } ]
				 } );	
        propertiesTaskNodeDialog.render();
        
        //Instantiate the event node property dialog 
    	processEventDialog = new YAHOO.widget.Dialog("ProcessEventDialog", 
				{ width : "700px",
				  fixedcenter : true,
				  visible : false,    
                  constraintoviewport : true,
                  modal : true,
				  buttons : [ { text:"确定", handler:taskSend, isDefault:true },
						      { text:"取消", handler:handleCancel } ]
				 } );	
        processEventDialog.render();
        
        //Instantiate the decision node property dialog
    	propertiesDecisionNodeDialog = new YAHOO.widget.Dialog("PropertiesDecisionNodeDialog", 
				{ width : "700px",
				  fixedcenter : true,
				  visible : false,    
                  constraintoviewport : true,
                  modal : true,
				  buttons : [ { text:"确定", handler:decisionSend, isDefault:true },
						      { text:"取消", handler:handleCancel } ]
				 } );	
        propertiesDecisionNodeDialog.render();
        
        //Instantiate the fork node property dialog
        propertiesForkNodeDialog = new YAHOO.widget.Dialog("PropertiesForkNodeDialog", 
                { width : "700px",
                  fixedcenter : true,
                  visible : false,    
                  constraintoviewport : true,
                  modal : true,
				  buttons : [ { text:"确定", handler:forkSend, isDefault:true },
						      { text:"取消", handler:handleCancel } ]
                 } ); 
        propertiesForkNodeDialog.render();


        propertiesJoinNodeDialog = new YAHOO.widget.Dialog("PropertiesJoinNodeDialog", 
                { width : "700px",
                  fixedcenter : true,
                  visible : false,    
                  constraintoviewport : true,
                  modal : true,
        		  buttons : [ { text:"确定", handler:joinSend, isDefault:true },
        					  { text:"取消", handler:handleCancel } ]
                 } ); 
        propertiesJoinNodeDialog.render();
 }



//
function setProcessDefinitionList(xmlhttp)
{
	var xmlResponse = xmlhttp.responseXML;
	if( xmlhttp.responseText.indexOf( "RsException" ) > 0 ){
		alertMessage("错误, 不能列出工作流。更多的信息请查看日志文件。");
		return;
	}
	
	try{
		processDefinitionList = xmlResponse.getElementsByTagName("name");
	}catch(e){
	   alertMessage(e.message);
	}
}
/*----------------------------dwr remote call api-----------------------*/
function getAllUsers(){
	//HrmsService.authenticationService_findAllUsers(function(oData){
	//	for(var i=0;i<oData.length;i++){
	//		var oOption = document.createElement("OPTION");
	//		oOption.text = oData[i].username;
	//		oOption.value = oData[i].username;		 
	//	 	$("users").add(oOption);			
	//	}		
	//});
}

function getAllGroups(){
	//HrmsService.authenticationService_findAllGroups(function(oData){
	//	for(var i=0;i<oData.length;i++){
	//		var oOption = document.createElement("OPTION");
	//		oOption.text = oData[i].groupname;
	//		oOption.value = oData[i].groupname;		 
	//	 	$("groups").add(oOption);			
	//	}		
	//});
}

/*------------------UI button event handler-----------------------*/
function exitwindow() {
	//HideAlertMessageDialog();
	var main = window.parent.parent.parent.document.getElementById("mainWindow")
	main.setAttribute("src", "Menu/GongZuoLiuGuanLi/main_wf.html");
}

//save process
function Save_Click(pdxml,schemaxml,wrapper){
	
	CtitJbpmAction.deployProcessDefinition(pdxml, schemaxml, function(o){
		alertMessage("工作流保存成功.");
		//wrapper.setChanged(false);
		//window.location = "workflow-designer.jsp";
	});
}

function displayDiv(me){
	var div = document.getElementById("actorDiv");
	var actors_label = document.getElementById("actors_label");
	var actors_user  = document.getElementById("actors_user");
	var groups = document.getElementById("groups");
	var users  = document.getElementById("users");
	if(me.value == "用户"){
		actors_label.innerText = "用户";
		actors_user.innerText = "请选择用户";
		getUsers(groups.value);
	}
	if(me.value == "组别"){
		actors_label.innerText = "组别";
		actors_user.innerText = "请选择组";
		getGroups();
	}
	div.style.display = "block";
}

function getUsers(name){
	MainAction.getUsersByGroupName(name, callback);
}

function callback(users){
	var userSel = document.getElementById("users");
	userSel.innerHTML = "";
	for(var i=0; i<users.length; i++){
		var user = users[i];
		var option = document.createElement("option");
		option.setAttribute("value", user);
		var label = document.createElement("label");
		label.innerText = user;
		option.appendChild(label);
		userSel.appendChild(option);
	}
}

function getGroups(){
	var userSel = document.getElementById("users");
	userSel.innerHTML = "";
	var array = new Array("受理人","承办人","审核人","审批人","办结人");
	for(var i=0; i<array.length; i++){
		var groupname = array[i];
		var option = document.createElement("option");
		option.setAttribute("value", groupname);
		var label = document.createElement("label");
		label.innerText = groupname;
		option.appendChild(label);
		userSel.appendChild(option);
	}
}
